package com.itheima.gamedemo2;

import java.util.*;

public class Room {
    //1.准备好54张牌，给房间使用，定义一个集合存储54张牌
    private List<Card> allCards = new ArrayList<>();
    //2.初始化54张牌 实例代码块
    {
        //3.准备花色
        String[] colors = {"♦","♣","♥","♠"};
        //4.准备点数
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K","A", "2"};
        int num = 0;
        //5.遍历 组合所有的花色和点数
        for(String size : sizes){
            for(String color : colors){
                num++;
                //6.创建牌对象
                Card card = new Card(size, color, num);
                //7.加入到集合中
                allCards.add(card);
            }
        }
        //8.添加大小王
//        allCards.add(new Card("", "🃏",++num));
//        allCards.add(new Card("", "👲",++num));
        Collections.addAll(allCards, new Card("", "🃏",++num), new Card("", "👲",++num));
    }


    /**
     * 启动游戏
     */
    public void start(){
        System.out.println("洗牌前：" + allCards);
        //9.洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后：" + allCards);
        //10.发牌
        Map<String, List<Card>> players = new HashMap<>();

        List<Card> xh = new ArrayList<>();
        players.put("小红", xh);

        List<Card> xm = new ArrayList<>();
        players.put("小明", xm);

        List<Card> xb = new ArrayList<>();
        players.put("小北", xb);

        for(int i = 0; i < allCards.size() - 3; i++){
            //获取到当前这张牌对象
            Card card = allCards.get(i);
            //判断这张牌属于谁
            if(i % 3 == 0){
                xh.add(card);
            }else if (i % 3 == 1){
                xm.add(card);
            }else if (i % 3 == 2){
                xb.add(card);
            }
        }

        //11.最后三张牌
        List<Card> lastCards = allCards.subList(allCards.size() - 3, allCards.size());
        //抢地主
        xb.addAll(lastCards);

        //12.排序
        sortCards(xh);
        sortCards(xm);
        sortCards(xb);

        //13.看牌 遍历Map集合
        for(Map.Entry<String, List<Card>> entry : players.entrySet()){
            //获取玩家名称
            String name = entry.getKey();
            //获取玩家牌
            List<Card> cards = entry.getValue();
            //遍历玩家牌
            System.out.println(name + "的牌是：" + cards);
        }

    }

    /**
     * 排序
     * @param cards
     */
    public void sortCards(List<Card> cards){
        Collections.sort(cards, (o1, o2) -> o2.getNum() - o1.getNum());
    }
}
